using ASP;
using HoWave.Common;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web;
using HoWave.Web.Shop;
using HoWave.Web.Shop.Member;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Member_JXC_OrderSale : Member, IRequiresSessionState
{
	private string action = "";
	private int infoOrderID = 0;
	private Shop_Product _shop_product = null;
	private bool ownerpower = false;
	protected ScriptManager ScriptManager1;
	protected DropDownList DroOrderStatic;
	protected DropDownList DropDeliverStatus;
	protected DropDownList DroPayStatic;
	protected TextBox txtBuyer;
	protected RadioButtonList rblSex;
	protected UpdatePanel up1;
	protected TextBox Contacter_Name;
	protected TextBox Contacter_ZipCode;
	protected Shop_UserControls_HowaveProvinceCity HoWaveProvinceCity1;
	protected TextBox Contacter_Address;
	protected TextBox Contacter_Email;
	protected TextBox Contacter_Phone;
	protected TextBox Contacter_Mobile;
	protected HiddenField hfProductID;
	protected HiddenField hfProductStr;
	protected HiddenField hfNeedInvioce;
	protected HiddenField hfDeliverCharge;
	protected HtmlGenericControl divProduct;
	protected Label lblGoodsTotal;
	protected Label lblInvoiceTotal;
	protected Label lblDeliverTotal;
	protected Label lblOrderTotal;
	protected Button btnSave;
	public string Action
	{
		get
		{
			if (base.Request.QueryString["Action"] != null)
			{
				this.action = base.Request.QueryString["Action"].Trim();
			}
			else
			{
				this.action = "";
			}
			return this.action;
		}
		set
		{
			this.action = value;
		}
	}
	public int InfoOrderID
	{
		get
		{
			if (base.Request.QueryString["infoOrderID"] != null)
			{
				this.infoOrderID = Convert.ToInt32(base.Request.QueryString["infoOrderID"].Trim());
			}
			return this.infoOrderID;
		}
		set
		{
			this.infoOrderID = value;
		}
	}
	protected Shop_Product shop_product
	{
		get
		{
			Shop_Product shop_product;
			if (this._shop_product == null)
			{
				this._shop_product = new Shop_Product(this.conn);
				shop_product = this._shop_product;
			}
			else
			{
				shop_product = this._shop_product;
			}
			return shop_product;
		}
	}
	public bool Ownerpower
	{
		get
		{
			if (base.OwnerPowerFinancemanage(base.user.UserID))
			{
				this.ownerpower = true;
			}
			return this.ownerpower;
		}
	}
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			this.rblSex.bindDataToRadioButtonList("HL_ClientContacter", "Sex", true);
			string text = this.Action;
			if (text != null)
			{
				if (!(text == "addinfo"))
				{
					if (!(text == "editinfo"))
					{
					}
				}
			}
		}
	}
	protected void btnSave_Click(object sender, EventArgs e)
	{
		Shop_PayType shop_PayType = new Shop_PayType(this.conn);
		DataTable offLinePayTypeByUserID = shop_PayType.GetOffLinePayTypeByUserID(base.user.UserID);
		if (offLinePayTypeByUserID.Rows.Count > 0)
		{
			int num = Convert.ToInt32(offLinePayTypeByUserID.Rows[0]["TypeID"].ToString());
			CreateOrder createOrder = new CreateOrder(this.conn);
			int userID = 0;
			createOrder.UserID = userID;
			createOrder.BuyerName = this.txtBuyer.Text;
			createOrder.SalerID = base.user.UserID;
			createOrder.SalerName = base.user.UserName;
			createOrder.ShopID = base.user.ShopID;
			createOrder.ContacterName = this.Contacter_Name.Text;
			createOrder.Province = this.HoWaveProvinceCity1.SelectedProvince;
			createOrder.City = this.HoWaveProvinceCity1.SelectedCity;
			createOrder.Address = this.Contacter_Address.Text;
			createOrder.ZipCode = this.Contacter_ZipCode.Text;
			createOrder.Email = this.Contacter_Email.Text;
			createOrder.Mobile = this.Contacter_Mobile.Text;
			createOrder.Phone = this.Contacter_Phone.Text;
			string text = DateTime.Now.ToString("yyyyMMddhhmmss") + Text.Random(6, 0);
			createOrder.OrderFormNum = text;
			int num2 = Convert.ToInt32(this.DroOrderStatic.SelectedValue);
			createOrder.Status = num2;
			int num3 = Convert.ToInt32(this.DropDeliverStatus.SelectedValue);
			createOrder.DeliverStatus = num3;
			int num4 = Convert.ToInt32(this.DroPayStatic.SelectedValue);
			createOrder.PayStatus = num4;
			createOrder.SelectedPayType = num;
			createOrder.DeliverCharge = this.hfDeliverCharge.Value;
			if (this.Action == "addinfo")
			{
				int num5 = createOrder.OrderAddHand(this.hfProductStr.Value, this.hfNeedInvioce.Value);
				if (num5 > 0)
				{
					this.UpdateProductCount(num5);
					this.AddClientInfo();
					if (num4 == 1)
					{
						OrderAction orderAction = new OrderAction(this.conn);
						orderAction.OffLinePayOrder(num5, num, "线下支付订单");
					}
					if (num2 == 1)
					{
						base.AddInfoItem(num5, this.DropDeliverStatus.SelectedValue, base.user.UserID);
					}
					if (num3 == 1)
					{
						Shop_DeliverItem shop_DeliverItem = new Shop_DeliverItem(this.conn);
						int deliverType = 0;
						string deliverName = "";
						string expressNumber = "";
						string deliverCompany = "";
						string userName = base.user.UserName;
						string remark = "";
						bool flag = shop_DeliverItem.AddDeliverItem(userID, base.user.UserID, num5, deliverType, deliverName, DateTime.Parse(DateTime.Now.ToLocalTime().ToString()), expressNumber, deliverCompany, userName, remark);
					}
					this.WriteSuccessMsg("", "添加成功！", "../Shop_BuyOrder.aspx?OrderFormNum=" + text);
				}
				else
				{
					this.WriteErrMsg("", "添加失败！", "");
				}
			}
		}
	}
	private void UpdateProductCount(int orderid)
	{
		if (orderid > 0)
		{
			DataTable dataTable = this.conn.GetDataTable("select oi.OrderFormID,oi.ProductID,Quantity,pt.ProductType from HLShop_OrderItem as oi,HLShop_Product as pt where oi.ProductID=pt.ProductID and OrderFormID=" + orderid);
			if (dataTable != null && dataTable.Rows.Count > 0)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					int num = Convert.ToInt32(dataTable.Rows[i]["ProductID"].ToString());
					int num2 = int.Parse(dataTable.Rows[i]["Quantity"].ToString());
					string a = dataTable.Rows[i]["ProductType"].ToString();
					string text = "";
					if (a == "1")
					{
						if (this.DropDeliverStatus.SelectedValue.Trim() != "0")
						{
							text = string.Concat(new object[]
							{
								"update HLShop_Product set BuyTimes=BuyTimes+1,Stocks=Stocks-",
								num2,
								" where ProductID=",
								num
							});
						}
					}
					else
					{
						if (a == "4" || a == "5")
						{
							text = string.Concat(new object[]
							{
								"update HLShop_Product set BuyTimes=BuyTimes+1,Stocks=Stocks-",
								num2,
								" where ProductID=",
								num
							});
						}
					}
					if (text != "")
					{
						this.conn.Execute(text);
					}
				}
			}
		}
	}
	private void AddClientInfo()
	{
		if (base.OwnerPowerClientRelation(base.user.UserID))
		{
			int userID = base.user.UserID;
			string text = this.txtBuyer.Text;
			string text2 = this.Contacter_Name.Text;
			string type = "outlinebuy";
			HoWave.Data.Model.Client clientByClientName = HoWave.Data.Bll.Client.instance.GetClientByClientName(text, userID);
			if (clientByClientName != null)
			{
				int clientID = clientByClientName.ClientID;
				bool flag = base.UpdateClientInfoModel(clientByClientName, text2, type);
				if (flag)
				{
					HoWave.Data.Model.ClientContacter contacterByClientId = HoWave.Data.Bll.ClientContacter.instance.GetContacterByClientId(clientID, userID);
					if (contacterByClientId != null)
					{
						HoWave.Data.Model.ClientContacter clientContacter = this.ClientContacterInfo(text, text2, userID);
						clientContacter.UpdateTime = new DateTime?(DateTime.Now);
						clientContacter.ClientID = new int?(clientID);
						clientContacter.ContacterID = contacterByClientId.ContacterID;
						bool flag2 = HoWave.Data.Bll.ClientContacter.instance.Update(clientContacter);
					}
					else
					{
						HoWave.Data.Model.ClientContacter clientContacter2 = this.ClientContacterInfo(text, text2, userID);
						clientContacter2.ClientID = new int?(clientID);
						HoWave.Data.Bll.ClientContacter.instance.Add(clientContacter2);
					}
				}
			}
			else
			{
				HoWave.Data.Model.Client clientInfoModel = base.GetClientInfoModel(text, text2, userID, type);
				int num = HoWave.Data.Bll.Client.instance.Add(clientInfoModel);
				if (num > 0)
				{
					HoWave.Data.Model.ClientContacter clientContacter2 = this.ClientContacterInfo(text, text2, userID);
					clientContacter2.ClientID = new int?(num);
					HoWave.Data.Bll.ClientContacter.instance.Add(clientContacter2);
				}
			}
		}
	}
	private HoWave.Data.Model.ClientContacter ClientContacterInfo(string UserName, string TrueName, int SalerID)
	{
		return new HoWave.Data.Model.ClientContacter
		{
			Owner = SalerID,
			UserName = UserName,
			TrueName = TrueName,
			Province = this.HoWaveProvinceCity1.SelectedProvince,
			City = this.HoWaveProvinceCity1.SelectedCity,
			Districts = this.HoWaveProvinceCity1.SelectedArea,
			Address = this.Contacter_Address.Text,
			ZipCode = this.Contacter_ZipCode.Text,
			HomePhone = this.Contacter_Phone.Text,
			Mobile = this.Contacter_Mobile.Text,
			Email = this.Contacter_Email.Text,
			Sex = Convert.ToInt32(this.rblSex.SelectedValue),
			Marriage = 3,
			CreateTime = new DateTime?(DateTime.Now),
			UpdateTime = new DateTime?(DateTime.Now)
		};
	}
	protected void DropDeliverStatus_SelectedIndexChanged(object sender, EventArgs e)
	{
		string a = this.DropDeliverStatus.SelectedValue.Trim();
		if (a == "1")
		{
			this.DroOrderStatic.SelectedValue = "1";
		}
	}
}
